package org.yystory.ui;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.yystory.dataobject.TopicObject;
import org.yystory.entity.Story;
import org.yystory.service.StoryService;
import org.yystory.utility.ErrorParser;

import com.alibaba.fastjson.JSON;

public class TopicServlet extends HttpServlet {

	private static final long serialVersionUID = 1849992141254772821L;

	public TopicServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response(request, response);
	}

	@SuppressWarnings("rawtypes")
	private void response(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		try {
			int follow;
			int userId;
			int length;
			byte[] data = null;
			FileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			List items = upload.parseRequest(request);
			Iterator iter = items.iterator();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				if (item.isFormField()) {
					String fieldName = item.getFieldName();
					String value = item.getString();
					request.setAttribute(fieldName, value);
				} else {
					if (data == null) {
						data = item.get();
					}
				}
			}

			follow = Integer
					.parseInt(request.getAttribute("follow").toString());
			userId = Integer.parseInt(request.getAttribute("uid").toString());
			length = Integer
					.parseInt(request.getAttribute("length").toString());
			TopicObject topicObject;
			if (follow == 0) {
				String name = request.getAttribute("name").toString();
				int storyId = StoryService.newTopic(name, userId, length, data);
				topicObject = new TopicObject();
				topicObject.setName(name);
				topicObject.setId(storyId);
				topicObject.setUserId(userId);
			} else {
				int sequenceId = StoryService.follow(follow, userId, length,
						data);
				topicObject = new TopicObject();
				Story story = StoryService.findStory(sequenceId);
				topicObject.setName(story.getName());
				topicObject.setId(story.getId());
				topicObject.setUserId(userId);
			}
			response.getWriter().print(JSON.toJSONString(topicObject));
			response.getWriter().close();
		} catch (Exception e) {
			response.getWriter().print(
					ErrorParser.parseError("TopicServlet", e.getClass()
							.getName() + ":" + e.getMessage()));
			response.getWriter().close();
		}
	}
}
